var e = require("../../../@babel/runtime/helpers/toConsumableArray"),
  t = require("../../../@babel/runtime/helpers/slicedToArray");
require("../../../@babel/runtime/helpers/Objectentries");
var r = require("../../../@babel/runtime/helpers/regeneratorRuntime"),
  a = require("../../../@babel/runtime/helpers/objectSpread2"),
  n = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  i = require("../../../utils/index"),
  s = require("../../../config/index"),
  c = i.helper.tabCommon,
  l = i.helper.location,
  o = i.tools.common,
  u = c.getmodeStorageKey(s.typeMode.recruit);
Component({
  externalClasses: ["custom-class"],
  useStore: function (e) {
    return {
      driverClasslist: e.global.driverClass,
      machineList: e.global.mechanicalClass,
      areaList: e.storage.areaTree
    }
  },
  properties: {
    type: {
      type: String,
      value: ""
    },
    value: {
      type: Array,
      value: []
    },
    max: {
      type: [Number, Array],
      value: 1
    },
    showSelected: {
      type: Boolean,
      value: !1
    },
    showAll: {
      type: Boolean,
      value: !1
    },
    showRoot: {
      type: Boolean,
      value: !0
    }
  },
  data: {
    hide: !1,
    list: [],
    active: {},
    current: [0],
    picked: []
  },
  observers: {
    active: function () {
      var e = this.getSelectItem();
      this.setData({
        picked: e
      })
    }
  },
  methods: {
    onCancel: function () {
      var e = this;
      this.setData({
        hide: !0
      }), setTimeout((function () {
        e.setData({
          hide: !1
        }), e.triggerEvent("close")
      }), 200)
    },
    onSubmit: function () {
      var e = this;
      return n(r().mark((function t() {
        var n, i, s;
        return r().wrap((function (t) {
          for (;;) switch (t.prev = t.next) {
            case 0:
              if (n = e.getSelectItem(), e.onCancel(), i = {
                  value: n,
                  label: n.map((function (e) {
                    return e.name
                  })).join()
                }, !e.data.isUpdateStorage) {
                t.next = 7;
                break
              }
              return s = a(a({}, c.getFilterParamsInfo(u)), {}, {
                type: i
              }), t.next = 7, c.setFilterParamsInfo(u, s);
            case 7:
              e.triggerEvent("change", i);
            case 8:
            case "end":
              return t.stop()
          }
        }), t)
      })))()
    },
    getSelectItem: function () {
      var e = this,
        r = this.data.active,
        a = [];
      return Object.entries(r).forEach((function (r) {
        var n = t(r, 2),
          i = n[0];
        n[1].forEach((function (t) {
          a.push(e.data.list[0][i].children[t])
        }))
      })), a
    },
    onClickItem: function (e) {
      var t, r = this.data,
        a = r.list,
        n = r.active,
        i = r.current,
        s = r.showAll,
        c = e.detail,
        l = c.item,
        o = c.index,
        u = c.level,
        h = 0 === (null === (t = l.children) || void 0 === t ? void 0 : t.length);
      if (l.children && l.children.length) a[u + 1] = l.children, i[u] = o;
      else {
        var d = n[i[u - 1]];
        if (d) {
          var v = d.indexOf(o); - 1 !== v ? d.splice(v, 1) : this.maxCheck(h) && (h || 0 == d[0] && s ? n[i[u - 1]] = [o] : d.push(o))
        } else this.maxCheck(h) && (n[i[u - 1]] = [o])
      }
      this.setData({
        active: n,
        current: i,
        list: a
      })
    },
    maxCheck: function (e) {
      var t = this.data.max,
        r = !Array.isArray(t),
        a = this.getSelectItem();
      if (r) return !(a.length + 1 > t && ($.msg("最多选择".concat(t, "个")), 1));
      var n = a.filter((function (e) {
          return !e.children
        })).length,
        i = a.length - n;
      return e && i + 1 > t[0] ? ($.jxAlert({
        content: "选择省或直辖市的个数超出限制"
      }), !1) : !(!e && n + 1 > t[1] && ($.jxAlert({
        content: "选择市的个数超出限制"
      }), 1))
    },
    onItemCancel: function (e) {
      var t = e.target.dataset.index,
        r = this.data,
        a = r.picked,
        n = r.list;
      a.splice(t, 1);
      var i = this.getActiveIndex(a, n[0]);
      this.setData({
        active: i
      })
    },
    getActiveIndex: function (e, t) {
      var r = {};
      return e.forEach((function (e) {
        var a = o.getTreeById(t, e.id),
          n = t.findIndex((function (e) {
            return a.pid === e.id
          })),
          i = r[n] || [],
          s = t[n].children.findIndex((function (t) {
            return e.id == t.id
          }));
        i.push(s), r[n] = i
      })), r
    }
  },
  lifetimes: {
    attached: function () {
      var e = this;
      return n(r().mark((function t() {
        var a, n, i, s, c, o, u, h, d;
        return r().wrap((function (t) {
          for (;;) switch (t.prev = t.next) {
            case 0:
              a = e.data, n = a.machineList, i = a.driverClasslist, s = a.areaList, c = a.type, o = a.showAll, u = a.showRoot, t.t0 = c, t.next = "area" === t.t0 ? 4 : "machine" === t.t0 ? 13 : 15;
              break;
            case 4:
              if (!o) {
                t.next = 10;
                break
              }
              return t.next = 7, l.transAreaData(s, u);
            case 7:
              t.t1 = t.sent, t.next = 11;
              break;
            case 10:
              t.t1 = s;
            case 11:
              return h = t.t1, t.abrupt("break", 16);
            case 13:
              return h = n, t.abrupt("break", 16);
            case 15:
              h = i;
            case 16:
              d = [h, h[0].children], e.setData({
                list: d
              });
            case 18:
            case "end":
              return t.stop()
          }
        }), t)
      })))()
    },
    ready: function () {
      var t = this.data,
        r = t.value,
        a = t.isUpdateStorage,
        n = t.list;
      console.log("value :>> ", r);
      var i = r || [];
      if (a) {
        var s = c.getFilterParamsInfo(u).type.value;
        s && (i = s)
      }
      if (i.length) {
        var l = this.getActiveIndex(i, n[0]),
          o = Object.keys(l).map((function (e) {
            return +e
          })),
          h = Math.min.apply(Math, e(o));
        n[1] = n[0][h].children, this.setData({
          active: l,
          list: n,
          current: [h]
        })
      }
    }
  }
});